package com.mall507.fivepeaks.widget.xdialog;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.mall507.fivepeaks.R;

import java.util.HashMap;
import java.util.List;

public class XDialogDemos {

	public Dialog myStyle1(Context context) {
		Dialog mDialog = new Dialog(context, R.style.XDialogSubmit);
		LayoutInflater in = LayoutInflater.from(context);
		View viewDialog = in.inflate(R.layout.view_xdialog_1, null);
		// 这里可以设置dialog的大小，当然也可以设置dialog title等
        //LayoutParams layoutParams = new LayoutParams(Util.getWindowWith(context) * 20 / 30,Util.getWindowWith(context) * 10 / 30);
        //mDialog.setContentView(viewDialog, layoutParams);
		mDialog.setContentView(viewDialog);
		// 点击对话框外部取消对话框显示
		mDialog.setCanceledOnTouchOutside(true);
		mDialog.show();
		return mDialog;
	}
	
	public Dialog myStyle2(Context context) {
		Dialog mDialog = new Dialog(context, R.style.XDialog_my2);
		LayoutInflater in = LayoutInflater.from(context);
		View viewDialog = in.inflate(R.layout.view_xdialog_2, null);
		// 这里可以设置dialog的大小，当然也可以设置dialog title等
        //LayoutParams layoutParams = new LayoutParams(Util.getWindowWith(context) * 20 / 30,Util.getWindowWith(context) * 10 / 30);
        //mDialog.setContentView(viewDialog, layoutParams);
		mDialog.setContentView(viewDialog);
		// 点击对话框外部取消对话框显示
		mDialog.setCanceledOnTouchOutside(true);
		mDialog.show();
		return mDialog;
	}
	
	public Dialog myStyle3(Context context,String msg) {
		Dialog mDialog = new Dialog(context, R.style.XDialog_my3);
		LayoutInflater in = LayoutInflater.from(context);
		View viewDialog = in.inflate(R.layout.view_xdialog_3, null);
		((TextView)viewDialog.findViewById(R.id.textView)).setText(msg);
		mDialog.setContentView(viewDialog);
		//全屏
		mDialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
		// 点击对话框外部取消对话框显示
		mDialog.setCanceledOnTouchOutside(true);
		mDialog.show();
		return mDialog;
	}

	public void sysStyle1(Context context) {
		AlertDialog.Builder builder = new Builder(context);
		builder.setMessage("确认退出吗？");
		builder.setTitle("提示");
		builder.setPositiveButton("确认", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.setNegativeButton("取消", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				dialog.dismiss();
			}
		});
		builder.create().show();
	}
	
	public void sysStyle2(final Context context) {
		Dialog dialog = new AlertDialog
				.Builder(context)
		        .setIcon(android.R.drawable.btn_star)
		        .setTitle("喜好调查")
		        .setMessage("你喜欢李连杰的电影吗？")
		        .setPositiveButton("很喜欢",new OnClickListener() {
		                    @Override
		                    public void onClick(DialogInterface dialog, int which) {
			                   Toast.makeText(context, "我很喜欢他的电影。",Toast.LENGTH_LONG).show();}
		                    })
		        .setNegativeButton("不喜欢", new OnClickListener() {
			                @Override
			                public void onClick(DialogInterface dialog, int which) {
			                   Toast.makeText(context, "我不喜欢他的电影。", Toast.LENGTH_LONG).show();}
			                })
			    .setNeutralButton("一般", new OnClickListener() {
			                @Override
			                public void onClick(DialogInterface dialog, int which) {
			                   Toast.makeText(context, "谈不上喜欢不喜欢。", Toast.LENGTH_LONG).show();}
			                })
			    .create();
		dialog.show();
	}
	
	public void sysStyle3(Context context) {
		new AlertDialog
		     .Builder(context)
		     .setTitle("请输入")
		     .setIcon(android.R.drawable.ic_dialog_info)
		     .setView(new EditText(context))
		     .setPositiveButton("确定", null)
			 .setNegativeButton("取消", null)
			 .show();
	}
	
	/**
	 * 复选框
	 * @param context
	 * @param title 弹出框标题
	 * @param items 复选项{name名称  value值  isSelected是否选中(不传默认未选中)}
	 * */
	public void sysStyle4(Context context,String title,List<HashMap<String, Object>> items) {
		final String[] itemNames = new String[items.size()];//名称
		final String[] itemValues = new String[items.size()];//值
		final boolean[] isSelected = new boolean[items.size()];//是否选中
		for(int i = 0;i<items.size();i++){
			itemNames[i]=(String) items.get(i).get("name");
			itemValues[i]=(String) items.get(i).get("value");
			if(null==items.get(i).get("isSelected")){
			   isSelected[i]=false;//默认未选中
			}
		}
		
        Dialog alertDialog = new AlertDialog.Builder(context)
                .setTitle(title)
                .setIcon(R.mipmap.ic_launcher)   
                .setMultiChoiceItems(itemNames, isSelected, new DialogInterface.OnMultiChoiceClickListener() {   
                    @Override   
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {   
                    	isSelected[which] = isChecked;   
                    }   
                })
                .setPositiveButton("确认", new DialogInterface.OnClickListener() {   
                    @Override   
                    public void onClick(DialogInterface dialog, int which) {   
                        StringBuilder stringBuilder = new StringBuilder();   
                        for (int i = 0; i < isSelected.length; i++) {   
                            if (isSelected[i] == true)   
                            {   
                                stringBuilder.append(itemNames[i] + "、");   
                            }   
                        }   
                    }   
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {   
   
                    @Override   
                    public void onClick(DialogInterface dialog, int which) {   
                    }   
                })
                .create();   
        alertDialog.show();
	}
	
	public void sysStyle5(Context context) {
		new AlertDialog
		     .Builder(context)
		     .setTitle("单选框")
		     .setIcon(android.R.drawable.ic_dialog_info)
		     .setSingleChoiceItems(new String[] { "Item1", "Item2" },0,new DialogInterface.OnClickListener() {
			        public void onClick(DialogInterface dialog, int which) {
			                dialog.dismiss();}
			  })
			 .setNegativeButton("取消", null)
			 .show();
	}
	
	
	public void sysStyle6(Context context) {
		new AlertDialog
		     .Builder(context)
		     .setTitle("列表框")
		     .setItems(new String[] { "Item1", "Item2" }, null)
		     .setNegativeButton("确定", null).show();
	}
	
}
